package com.ruoyi.front_desk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.front_desk.Mapper.FrontdeskNameCategoryMapper;
import com.ruoyi.front_desk.domain.FrontdeskNameCategory;
import com.ruoyi.front_desk.service.FrontdeskNameCategoryService;
import com.ruoyi.name_category.domain.NameCategory;
import com.ruoyi.name_category.service.INameCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class FrontdeskNameCategoryImpl  extends ServiceImpl<FrontdeskNameCategoryMapper,FrontdeskNameCategory> implements FrontdeskNameCategoryService {
    @Resource
    public FrontdeskNameCategoryMapper frontdeskNameCategoryMapper;




    public List<FrontdeskNameCategory> getNameCategoryById(Integer id) {

        return frontdeskNameCategoryMapper.FrontdeskgetNameCategoryById(id);
    }
    @Override
    public List<FrontdeskNameCategory> getAllCategories() {
        return frontdeskNameCategoryMapper.selectAllCategories();
    }


    @Override
    public List<FrontdeskNameCategory> listTree() {
        List<FrontdeskNameCategory> exer = getAllCategories();
        //List<FrontdeskNameCategory> exer = baseMapper.selectList(null);
       // List<NameCategory> exer = nameCategoryService.selectNameCategoryList(FrontdeskNameCategory);
// List<FrontdeskNameCategory> exer = nameCategoryService.selectNameCategoryList(nameCategory);
        List<FrontdeskNameCategory> CategoryTree = exer.stream().filter(
                FrontdeskNameCategory -> {
                    return FrontdeskNameCategory.getParentId() == 0;
                }
        ).map(FrontdeskNameCategory -> {
                    FrontdeskNameCategory.setChildrenCategory(getChildrenCategories(FrontdeskNameCategory,exer));
                    return FrontdeskNameCategory;
                }
        ).collect(Collectors.toList());

        return CategoryTree;
    }

    private List<FrontdeskNameCategory> getChildrenCategories(FrontdeskNameCategory root, List<FrontdeskNameCategory> all){
        List<FrontdeskNameCategory> children = all.stream().filter(frontdeskNameCategory -> {
            return frontdeskNameCategory.getParentId() == root.getId();
        }).map(frontdeskNameCategory -> {
            frontdeskNameCategory.setChildrenCategory(getChildrenCategories(frontdeskNameCategory, all));
            return frontdeskNameCategory;
        }).collect(Collectors.toList());
        return children;
    }
    @Override
    public List<FrontdeskNameCategory> exer() {
        List<FrontdeskNameCategory> exer = baseMapper.selectList(null);
        return exer;
    }




    public static FrontdeskNameCategory getCategoryById(List<FrontdeskNameCategory> categories, Integer id) {
        for (FrontdeskNameCategory category : categories) {
            if (category.getId().equals(id)) {
                return category;
            }
            FrontdeskNameCategory result = getCategoryById(category.getChildrenCategory(), id);
            if (result != null) {
                return result;
            }
        }
        return null;
    }
    @Override
    public FrontdeskNameCategory selectcategorybyid(Integer id) {
        List<FrontdeskNameCategory> frontdeskNameCategories = listTree();
        // List<FrontdeskNameCategory> result = new ArrayList<>();
        FrontdeskNameCategory categoryById = getCategoryById(frontdeskNameCategories, id);
        return categoryById;
    }

    @Override
    public List<Integer> selectnamebyid(Integer id) {
        List<FrontdeskNameCategory> frontdeskNameCategories = listTree();
        //List<FrontdeskNameCategory> result = new ArrayList<>();
       // Map<Integer, String> result = new HashMap<>();

        FrontdeskNameCategory categoryById = getCategoryById(frontdeskNameCategories, id);
       //List<FrontdeskNameCategory> childrenCategory = categoryById.getChildrenCategory();
        List<Integer> allChildrenIds = categoryById.getAllChildrenIds();
// 打印所有子级的 id
        for (Integer id2 : allChildrenIds) {
            System.out.println("Child ID: " + id2);

        }
        return allChildrenIds;
    }

    }

